﻿using System;
using System.Data.SqlClient;
using DataCenter.Component.Manager;
using DataCenter.Interface;
using DataCenter.Interface.Model;
using DataCenter.Share;
using System.Diagnostics;

namespace DataCenter.App
{
    public class AppEntry : IDisposable
    {
        public AppEntry(SqlCommand command)
        {
            this._Command = command;
            this._TableSetting = TableSetting.GetInstance();
            this._TableSetting.Initialize(command);
        }
        private SqlCommand _Command;
        private TableSetting _TableSetting;
        private string ObjectIds;
        private ExecTypes OperType;
        private DateTime Time0;
        private DateTime Time1;
        private EntityCollection Collection;

        public bool Initialize(string objectIds, ExecTypes execType, DateTime time0, DateTime time1)
        {
            this.ObjectIds = objectIds;
            this.OperType = execType;
            this.Time0 = time0;
            this.Time1 = time1;

            if (Time0 > Time1) return false;
            if (Time0 > Utils.NOW) return false;
            if (Time1 > Utils.NOW) Time1 = Utils.NOW;
            else Time1 = Utils.GetLastTime(Time1);
            Time0 = Time0.Date;

            Binary.CreateCache(CacheStream.BYTES_PER_MB);

            Collection = new EntityCollection(_Command);
            Collection.Initialize(ObjectIds);

            return true;
        }

        public bool Execute()
        {
            if (IsInOper(ExecTypes.Import))
            {
                Logger.GetInstance().Debug("[数据导入开始]");
                Stopwatch watch = new Stopwatch();
                watch.Start();
                using (IManager importManager = new ImportManager(_Command, Collection, IsInOper(ExecTypes.Process)))
                {
                    if (!importManager.Initialize(Time0, Time1)) return false;
                    if (!importManager.Execute()) return false;
                }
                watch.Stop();
                Logger.GetInstance().Debug("导入用时：" + watch.ElapsedMilliseconds);
                Logger.GetInstance().Debug("[数据导入结束]");
            }
            if (IsInOper(ExecTypes.Process))
            {
                Logger.GetInstance().Debug("[数据处理开始]");
                Stopwatch watch = new Stopwatch();
                watch.Start();
                using (IManager interpolateManager = new InterpolateManager(_Command, Collection, IsInOper(ExecTypes.Calculate)))
                {
                    if (!interpolateManager.Initialize(Time0, Time1)) return false;
                    if (!interpolateManager.Execute()) return false;
                }
                watch.Stop();
                Logger.GetInstance().Debug("处理用时：" + watch.ElapsedMilliseconds);
                Logger.GetInstance().Debug("[数据处理结束]");
            }
            if (IsInOper(ExecTypes.Calculate) && Collection.VirtualObjectCount > 0)
            {
                Logger.GetInstance().Debug("[数据计算开始]");
                Stopwatch watch = new Stopwatch();
                watch.Start();
                using (IManager calculateManager = new CalculateManager(_Command, Collection))
                {
                    if (!calculateManager.Initialize(Time0, Time1)) return false;
                    if (!calculateManager.Execute()) return false;
                }
                watch.Stop();
                Logger.GetInstance().Debug("计算用时：" + watch.ElapsedMilliseconds);
                Logger.GetInstance().Debug("[数据计算结束]");
            }

            return true;
        }

        private bool IsInOper(ExecTypes execType)
        {
            return (OperType & execType) == execType;
        }

        #region IDisposable Members

        public void Dispose()
        {
            Binary.ReleaseCache();
        }

        #endregion
    }
}
